package com.ly.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ly.bean.EasybuyProductCategory;
import com.ly.bean.MonthCost;
import com.ly.mapper.EasybuyProductCategoryMapper;
import com.ly.service.ProductCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
@author 李岩
@date 2021/12/14-13:52
@describe
*/
@Component
@Service(interfaceClass = ProductCategoryService.class)
public class ProductCategoryServiceImpl implements ProductCategoryService {
    private final Logger logger= LoggerFactory.getLogger(ProductCategoryServiceImpl.class);
    private ProductCategoryServiceImpl(){
        System.out.println("ProductCategoryServiceImpl   的service层");
    }
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private EasybuyProductCategoryMapper productCategoryMapper;
    //根据父级id查询下级分类  （0，1，2，3）
    public List<EasybuyProductCategory> findCategoryByPid(Integer pid) throws Exception{
        //如果redis中没有数据，那么就从数据库中搜索数据放到redis中
        if (redisTemplate.boundHashOps("productCategoryList").get("productCategoryList_"+pid)!=null){
            //如果redis中存在数据，那么就从redis中获取数据
            List<EasybuyProductCategory> list =(List<EasybuyProductCategory>) redisTemplate.boundHashOps("productCategoryList").get("productCategoryList_" + pid);
            logger.info("从redis中获取的数据   "+list);
            return list;
        }
        List<EasybuyProductCategory> list = productCategoryMapper.findCategoryByPid(pid);
        logger.info("从数据库中获取的数据   "+list);
        redisTemplate.boundHashOps("productCategoryList").put("productCategoryList_"+pid,list);
        return list;
    }
    /**
     * 根据一级商品的id查询下面所有的三级商品
     */
    @Override
    public List<EasybuyProductCategory> findThirdCategoryByPid(Integer pid) throws Exception {
        //如果redis中没有数据，那么就从数据库中搜索数据放到redis中
        if (redisTemplate.boundHashOps("productThirdCategoryList").get("productThirdCategoryList"+pid)!=null){
            //如果redis中存在数据，那么就从redis中获取数据
            List<EasybuyProductCategory> list =(List<EasybuyProductCategory>) redisTemplate.boundHashOps("productThirdCategoryList").get("productThirdCategoryList_" + pid);
            logger.info("从redis中获取的数据   "+list);
            return list;
        }
        List<EasybuyProductCategory> list = productCategoryMapper.findThirdCategoryByPid(pid);
        logger.info("从数据库中获取的数据   "+list);
        redisTemplate.boundHashOps("productThirdCategoryList").put("productThirdCategoryList_"+pid,list);
        return list;
    }
    //各种销售商品分类统计的饼状图实现
    @Override
    public List<EasybuyProductCategory> selectSellNameAndValue() throws Exception {
        return productCategoryMapper.selectSellNameAndValue();
    }

    @Override
    public List<MonthCost> selectMonthCost(String month) throws Exception {
        Map<String, Double> map = productCategoryMapper.selectMonthCost(month);
        Set<String> keys = map.keySet();
        List<MonthCost> list = new ArrayList<>();
        for (String key : keys) {
            MonthCost monthCost = new MonthCost();
            monthCost.setName(key);
            monthCost.setCosts(map.get(key));
            list.add(monthCost);
        }
        for (MonthCost monthCost : list) {
            System.out.println("list  "+monthCost);
        }

        return list;
    }
}
